import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:laos_mall/common/style/app_style.dart';
import 'package:laos_mall/common/style/extension_util.dart';
import 'package:laos_mall/common/utils/storage_util.dart';
import 'package:laos_mall/common/widght/mall/zb_Item_widget.dart';
import 'package:laos_mall/pages/main/config.dart';

enum LanguageDialogType {
  none,
  language,
  currency,
}

class LanguageDialog extends StatefulWidget {
  const LanguageDialog({super.key, this.type = LanguageDialogType.none});

  final LanguageDialogType? type;

  @override
  State<LanguageDialog> createState() => _LanguageDialogState();
}

class _LanguageDialogState extends State<LanguageDialog> {
  // final languages = ["LAO  -  LO", "中文 - ZH", "ENGLISH - EN"];
  final languages = Config.languages;
  final currencies = Config.currencies;

  int langIndex = 0;
  int currencyIndex = 0;

  @override
  void initState() {
    langIndex = languages.indexWhere((element) => element.value == Config.currentLanguage.value);
    langIndex = languages.indexWhere((element) => element.value == Config.currentLanguage.value);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [_buildHeader(0, languages).addVisibility(widget.type !=  LanguageDialogType.currency), _buildHeader(1, currencies).addVisibility(widget.type !=  LanguageDialogType.language)],
    ).addContainer(color: AppColor.white);
  }

  Widget _buildHeader(int itemIndex, List<LabelValue> dataSource) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(itemIndex == 0 ? 'sel_lang'.tr : 'sel_currency'.tr).addPadding(
            EdgeInsets.only(left: 20.w, top: 20.w, right: 20.w, bottom: 10.w)),
        ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          itemBuilder: (context, index) {
            return ZBItemWidget(
              style: ZBItemStyle.selectBg,
              prefixItem: itemIndex == 0
                  ? dataSource[index].label
                  : dataSource[index].label,
              suffixItem: "",
              isSel: itemIndex == 0
                  ? (index == langIndex)
                  : (index == currencyIndex),
            ).addGestureTap(() {
              var code = dataSource[index].value;
              if (itemIndex == 0) {

                langIndex = index;
                Get.updateLocale(Locale(code));
                Config.currentLanguage.value = code;
                StorageUtils().setLanguage(code);
              } else {
                currencyIndex = index;
                Config.currentCurrency.value = code;
                StorageUtils().setCurrency(code);
              }
              setState(() {});
              Get.back();
            });
          },
          itemCount: dataSource.length,
          shrinkWrap: true,
        ).addPadding(EdgeInsets.symmetric(horizontal: 20.w))
      ],
    ).addContainer(color: Colors.transparent);
  }
}
